﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class DireccionesDB

    Public Shared Function GetTipoDirecciones() As DataSet
        Dim cmd As New SqlCommand("spGetTipoDirecciones", DB.Conexion)
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        cmd.CommandType = CommandType.StoredProcedure
        da.SelectCommand = cmd
        da.Fill(ds, "TipoDirecciones")
        Return ds
    End Function

    Public Shared Sub InsertarDireccionPersona(ByVal id_tipo_documento As Integer, ByVal nro_documento As Integer, ByVal calle As String, ByVal barrio As String, ByVal id_tipo_direccion As Integer, ByVal id_localidad As Integer)
        Dim cmd As New SqlCommand("spInsertarDireccionPersona", DB.Conexion)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_tipo_documento", id_tipo_documento)
        cmd.Parameters.AddWithValue("@nro_documento", nro_documento)
        cmd.Parameters.AddWithValue("@calle", calle)
        cmd.Parameters.AddWithValue("@barrio", barrio)
        cmd.Parameters.AddWithValue("@id_tipo_direccion", id_tipo_direccion)
        cmd.Parameters.AddWithValue("@id_localidad", id_localidad)
        cmd.Parameters.Add("@Error", SqlDbType.Int).Direction = ParameterDirection.Output
        cmd.Parameters.Add("@ErrorDesc", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output

        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        If cmd.Parameters("@error").Value <> 0 Then
            Throw New ApplicationException(cmd.Parameters("@ErrorDesc").Value.ToString())
        End If


    End Sub

    Public Shared Sub ActualizarDireccionPersona(ByVal id_direccion As Integer, ByVal id_tipo_documento As Integer, ByVal nro_documento As Integer, ByVal calle As String, ByVal barrio As String, ByVal id_tipo_direccion As Integer, ByVal id_localidad As Integer)
        Dim cmd As New SqlCommand("spActualizarDireccionPersona", DB.Conexion)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_direccion", id_direccion)
        cmd.Parameters.AddWithValue("@id_tipo_documento", id_tipo_documento)
        cmd.Parameters.AddWithValue("@nro_documento", nro_documento)
        cmd.Parameters.AddWithValue("@calle", calle)
        cmd.Parameters.AddWithValue("@barrio", barrio)
        cmd.Parameters.AddWithValue("@id_tipo_direccion", id_tipo_direccion)
        cmd.Parameters.AddWithValue("@id_localidad", id_localidad)
        cmd.Parameters.Add("@Error", SqlDbType.Int).Direction = ParameterDirection.Output
        cmd.Parameters.Add("@ErrorDesc", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output

        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        If cmd.Parameters("@error").Value <> 0 Then
            Throw New ApplicationException(cmd.Parameters("@ErrorDesc").Value.ToString())
        End If


    End Sub

    Public Shared Sub EliminarDireccionPersona(ByVal id_tipo_documento As Integer, ByVal nro_documento As Integer, ByVal id_direccion As Integer)
        Dim cmd As New SqlCommand("spDeleteDireccionPersona", DB.Conexion)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_tipo_documento", id_tipo_documento)
        cmd.Parameters.AddWithValue("@nro_documento", nro_documento)
        cmd.Parameters.AddWithValue("@id_direccion", id_direccion)

        cmd.Parameters.Add("@Error", SqlDbType.Int).Direction = ParameterDirection.Output
        cmd.Parameters.Add("@ErrorDesc", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output

        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        If cmd.Parameters("@error").Value <> 0 Then
            Throw New ApplicationException(cmd.Parameters("@ErrorDesc").Value.ToString())
        End If


    End Sub

End Class
